package com.frame.library.skin.support;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * Added by Tom on 2024/07/02.
 */
public class SkinPreUtils {
    // volatile 防止指令重排序; 原子性、可见性
    private volatile static SkinPreUtils mInstance; // 单例
    private Context mContext; // MainActivity ?

    /**
     * 这里的context必须调用 context.getApplicationContext() 否则内存泄露
     */
    private SkinPreUtils(Context context) {
        this.mContext = context.getApplicationContext();
    }

    public static SkinPreUtils getInstance(Context context) {
        if (mInstance == null) {
            synchronized (SkinPreUtils.class) {
                if (mInstance == null) {
                    mInstance = new SkinPreUtils(context);
                }
            }
        }
        return mInstance;
    }


    /**
     * 保存当前皮肤路径
     *
     * @param skinPath 皮肤的路径path
     * String skinPath = Environment.getExternalStorageDirectory().getAbsolutePath()
     *                 + File.separator + "red.skin";
     */
    public void saveSkinPath(String skinPath) {
        SharedPreferences sp = mContext.getSharedPreferences(SkinConfig.SKIN_INFO_NAME, Context.MODE_PRIVATE);
        sp.edit().putString(SkinConfig.SKIN_PATH_NAME, skinPath).apply();
    }

    /**
     * 获取皮肤的路径
     *
     * @return 当前皮肤路径
     */
    public String getSkinPath() {
        SharedPreferences sp = mContext.getSharedPreferences(SkinConfig.SKIN_INFO_NAME, Context.MODE_PRIVATE);
        return sp.getString(SkinConfig.SKIN_PATH_NAME, "");
    }

    /**
     * 清空皮肤路径
     */
    public void clearSkinInfo() {
        saveSkinPath("");
    }

    public boolean needChangeSkin() {
        return false;
    }
}
